using UnityEngine;
using System.Collections;
using System.Collections.Generic;

//[RequireComponent( typeof( CustomGravity ) )]
[RequireComponent( typeof( AnimationBlend ) )]
[RequireComponent( typeof( Rigidbody ) )]
[RequireComponent( typeof( CapsuleCollider ) )]

public class Character : MonoBehaviour
{
	public Transform		_hookPrefab;
	public float			_speed = 6.0f;
    public float            _dampingWhileFalling = 0.02f;
	public float			_dampingOnGround = 0.3f;
	public float			_intensity = 3.0f;
    public float            _jumpForce = 4.0f;
	public string			_name;
    public Transform        _currentPlanet;

	private float				_xScale = 1.0f;
	private Vector3			_gravitationalPull = Vector3.zero;
	private Vector3			_moveDirection = Vector3.zero;
	private Vector3			_currentNormal;

    private RaycastHit		_raycastHitOnGround;
    private RaycastHit		_raycastHitWhileFalling;
    private Vector3			_objToPlanetDirection;
	

	void Start()
	{
		_currentNormal = transform.up;
        gameObject.rigidbody.collisionDetectionMode = CollisionDetectionMode.ContinuousDynamic;
        gameObject.rigidbody.useGravity = false;
        gameObject.rigidbody.freezeRotation = true;
        _xScale = gameObject.transform.localScale.x;
	}

    void Awake()
    {
        
    }

    void Update()
    {
        transform.position = new Vector3(transform.position.x, transform.position.y, 0.0f);
    }

	// Update is called once per frame
	void FixedUpdate()
	{
        Quaternion quaternionRotation;
        if (!gameObject.rigidbody.isKinematic)
        {
            _objToPlanetDirection = (_currentPlanet.transform.position - gameObject.transform.position).normalized;
            Physics.Raycast(transform.position, _objToPlanetDirection, out _raycastHitWhileFalling, 100.0f);

            Vector3 objToPlanetDirection = (_currentPlanet.transform.position - gameObject.transform.position).normalized;
            _currentNormal = Vector3.Lerp(_currentNormal, _raycastHitWhileFalling.normal, _dampingWhileFalling);
            quaternionRotation = Quaternion.FromToRotation(transform.up, _currentNormal);
            transform.rotation = quaternionRotation * transform.rotation;

            _moveDirection = new Vector3(Input.GetAxis("Horizontal_L"), 0, 0);
            _moveDirection = transform.TransformDirection(_moveDirection);

            gameObject.rigidbody.AddForce(_moveDirection * 5.0f, ForceMode.Force);
           
        }
        else
        {
            _objToPlanetDirection = (_currentPlanet.transform.position - gameObject.transform.position).normalized;
            Physics.Raycast(transform.position, _objToPlanetDirection, out _raycastHitOnGround, 10.0f);
            // print(_raycastHitOnGround.distance);

            float _inputDirection = Input.GetAxis("Horizontal_L");
            _moveDirection = new Vector3(_inputDirection, 0, 0);
            _moveDirection = transform.TransformDirection(_moveDirection);
            _moveDirection *= _speed;

            _currentNormal = Vector3.Lerp(_currentNormal, new Vector3(_raycastHitOnGround.normal.x, _raycastHitOnGround.normal.y, 0.0f), _dampingOnGround);

            quaternionRotation = Quaternion.FromToRotation(transform.up, _currentNormal);
            //quaternionRotation.SetAxisAngle(transform.worldToLocalMatrix * transform.up, 0.0f);
            transform.rotation = quaternionRotation * transform.rotation;
            transform.rotation.SetAxisAngle(transform.up, 0.0f);

            _objToPlanetDirection *= -1.0f;
            gameObject.transform.position = _raycastHitOnGround.point + _objToPlanetDirection;
            gameObject.transform.position += new Vector3(_moveDirection.x, _moveDirection.y, 0.0f) * Time.deltaTime;

            /*
            if (_inputDirection > 0.0f)
            {
                transform.localScale = new Vector3(_xScale, transform.localScale.y, transform.localScale.z);
            }
            else if (_inputDirection < 0.0f)
            {
                transform.localScale = new Vector3(-_xScale, transform.localScale.y, transform.localScale.z);
            }
             * */

            if( Input.GetButtonDown("Jump") && gameObject.rigidbody.isKinematic )
            {
                print("lkasdjflk");
                gameObject.rigidbody.isKinematic = false;
                Vector3 _fwdForce = (transform.localToWorldMatrix * (new Vector3(1.0f * Input.GetAxis("Horizontal_L"), 1.0f, 0.0f)));

                _fwdForce = Vector3.Project(_fwdForce, gameObject.transform.right);
                float _finalJumpForce = _jumpForce;
                if (Input.GetButton("BadComboButton"))
                {
                    print("BadComboButton");
                    _finalJumpForce *= 1.5f;
                }

                _fwdForce.Normalize();
                //_fwdForce *= _moveDirection.magnitude;
                gameObject.rigidbody.AddForce(gameObject.transform.up.x * _jumpForce, gameObject.transform.up.y * _finalJumpForce, 0.0f, ForceMode.Impulse);
                gameObject.rigidbody.AddForce(_fwdForce.x * _finalJumpForce, _fwdForce.y * _finalJumpForce, 0.0f, ForceMode.Impulse);
            }
        }
	}

	void OnCollisionEnter( Collision collision )
	{
        gameObject.rigidbody.isKinematic = true;
	}

	void OnCollisionStay( Collision collision )
	{
		
	}

	void OnCollisionExit( Collision collision )
	{
		
	}
}
